package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

import model.Methods;
import model.function;

public class Window extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Display display;
	private Number[] numbers;
	private Operator operators;
	private Methods op;
	private boolean 


	public Window (Methods op) {
		this.op = op;
		display = new Display(this.op); // The Display
		op.addObserver(display);
		numbers = new Number[10]; // We need 10 Numbers
		operators = new Operator();
		
		JPanel panelNumbers = new JPanel(); // Numbers Panel
		panelNumbers.setLayout(new GridLayout(3,3));
		
		// Generate the Number Buttons and add them to the panel.
		for(int i=1; i<=9; i++) {
			numbers[i] = new Number(i);
			panelNumbers.add(numbers[i]);
			numbers[i].addActionListener(this);
		}

		JPanel panelOperators = new JPanel(); // Operators Panel
		panelOperators.setLayout(new GridLayout(0,1));
		
		// Adding the Operators
		for (Operator value : operators.operator.values()) {
			panelOperators.add(value);
			value.addActionListener(this);
		}
		
		
		this.add(display, BorderLayout.NORTH);
		this.add(panelNumbers, BorderLayout.CENTER);
		this.add(panelOperators, BorderLayout.EAST);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400,400);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		
		if(e.getActionCommand().matches("\\d")) { // Check if Numbers
			display.append(e.getActionCommand().toString());
		}
		else {
			
		}
			
//		
//		if(secondValue && operation.equals("+")) {
//			op.v2 = Integer.parseInt(display.getText());
////			System.out.println(op.v2);
//			op.add();
//		}
//		
//		if(e.getActionCommand().matches("\\D") && !e.getActionCommand().matches("=")) { // check if Operators
//			op.v1 = Integer.parseInt(display.getText());
//			secondValue = true;
//			operation = e.getActionCommand().toString();
////			System.out.println(op.v1);
//			display.setText("0");
//		}
//		
//		if(e.getActionCommand().matches("=")) {
//			op.notifyObservers();
////			System.out.println(op.result);
//		}
	}

}
